<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Packet extends AdminBackEndController {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/packet_model', 'packet_model');
    }

    /**
     *@for ADMIN
     *@func Admin grid of available packet
     */
    public function index()
    {
        $aPacket = $this->packet_model->getAvailablePacket();
        $aReturn = array('pageTitle' => 'Gói sản phẩm', 'aPacket' => $aPacket);
        $this->renderView('backend/admin/packet/index', $aReturn);
    }

    /**
     *@for ADMIN
     *@func Admin insert packet
     */
    public function insert()
    {
        $packet = $this->input->post('packet', true);
        $this->form_validation->set_rules('packet[title]', 'lang:packet_title',
            'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('packet[product_count]',
            'lang:packet_product_count', 'trim|required|integer|xss_clean');
        $this->form_validation->set_rules('packet[price]', 'lang:packet_price',
            'trim|required|integer|max_length[6]|xss_clean');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('insert_fail'),
                'text' => '<ul>' . validation_errors() . '</ul>',
                );
            $this->renderJSON($aResult);
            return;
        }
        $packet['created_at'] = get_current_date();
        $result = $this->packet_model->addPacket($packet);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('insert_success'));
        } else {
            $aResult = array('code' => 0, 'text' => $this->lang->line('insert_fail'));
        }
        $this->renderJSON($aResult);
    }

    /**
     *@for ADMIN
     *@func Admin update packet status, info
     */
    public function update()
    {
        $id = 0;
        $tPacket = $this->input->post('packet');
        if (isset($tPacket['id']) && trim($tPacket['id']) != '') {
            $id = $this->encrypt->decode(trim($tPacket['id']));
            if ($id == false) {
                $aResult = array('code' => 0, 'title' => $this->lang->line('update_fail'));
                $this->renderJSON($aResult);
                return;
            }
        }

        $packet = $this->input->post('packet', true);

        if (isset($packet['active_flg'])) {
            //case check active
            if ($packet['active_flg'] == 0) {
                $packet['deleted_at'] = get_current_date();
            } else
                if ($packet['active_flg'] == 1) {
                    $packet['deleted_at'] = null;
                    $packet['updated_at'] = get_current_date();
                }
            unset($packet['active_flg']);
        } else {
            //case change info
            $this->form_validation->set_rules('packet[title]', 'lang:packet_title',
                'trim|required|max_length[100]|xss_clean');
            $this->form_validation->set_rules('packet[product_count]',
                'lang:packet_product_count', 'trim|required|integer|xss_clean');
            $this->form_validation->set_rules('packet[price]', 'lang:packet_price',
                'trim|required|integer|max_length[6]|xss_clean');
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            if ($this->form_validation->run() == false) {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('insert_fail'),
                    'text' => '<ul>' . validation_errors() . '</ul>',
                    );
                $this->renderJSON($aResult);
                return;
            } else {
                $packet['updated_at'] = get_current_date();
            }
        }
        unset($packet['id']);
        $result = $this->packet_model->updatePacket($id, $packet);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('update_success'));
        } else {
            $aResult = array('code' => 0, 'text' => $this->lang->line('update_fail'));
        }
        $this->renderJSON($aResult);
    }

    /**
     *@For ADMIN
     *@func Admin grid of checking if any shop register for new packet
     */
    public function register()
    {
        $active_flg = $this->input->get('active', true);
        if($active_flg!==false){
            if ($active_flg == 0) {
                $active_flg = 0;
            } elseif ($active_flg == 1) {
                $active_flg = 1;
            } else if ($active_flg == 2) {
                $active_flg = null;
            } else {
                show_404();
            }
        } else {
            $active_flg=null;
        }

        $aShopPacket = $this->packet_model->getRegisterPacket($active_flg);
        $aPacket = $this->packet_model->getAvailablePacket(array('a.id', 'a.title'));
        $this->load->model('repo/shop_model', 'shop_model');
        $aShop = $this->shop_model->getAllShop(array('a.id', 'a.title'));
        $aReturn = array(
            'pageTitle' => 'Gói sản phẩm - Đăng kí mới',
            'aShopPacket' => $aShopPacket,
            'aPacket' => $aPacket,
            'aShop' => $aShop);
        $this->renderView('backend/admin/packet/register', $aReturn);
    }

    /**
     *@For ADMIN
     *@func Admin grid of adding shop register for new packet
     */
    public function insertRegister()
    {
        $shop_id = 0;
        $packet_id = 0;
        $packet = $this->input->post('packet', true);
        if (isset($packet['id']) && trim($tPacket['id']) != '' && isset($packet['shop_id']) &&
            trim($tPacket['shop_id']) != '') {
            if ($packet['id'] == false || $packet['shop_id'] == false) {
                $aResult = array('code' => 0, 'title' => $this->lang->line('insert_fail'));
                $this->renderJSON($aResult);
                return;
            }
        }
        $packet['active_flg'] = 0;
        $packet['created_at'] = get_current_date();
        $result = $this->packet_model->buyPacket($packet);
        if ($result) {
            $aResult = array(
                'code' => 1,
                'text' => $this->lang->line('insert_success'),
                'created_at' => $packet['created_at']);
        } else {
            $aResult = array('code' => 0, 'text' => $this->lang->line('insert_fail'));
        }
        $this->renderJSON($aResult);
    }

    /**
     *@For ADMIN
     *@func Admin grid of checking if any shop register for new packet
     */
    public function updateRegister()
    {
        $shop_id = 0;
        $packet_id = 0;
        $shop_packet_id = 0;
        $packet = $this->input->post('packet', true);
        $tPacket = $this->input->post('packet');

        if (isset($tPacket['id']) && trim($tPacket['id']) != '' && isset($packet['shop_id']) &&
            trim($packet['shop_id']) != '' && isset($packet['packet_id']) && trim($packet['packet_id']) !=
            '') {
            $shop_packet_id = $this->encrypt->decode($tPacket['id']);
            if ($packet['id'] == false || $packet['shop_id'] == false || $packet['packet_id'] == false ||
                $shop_packet_id == false) {
                $aResult = array('code' => 0, 'title' => $this->lang->line('insert_fail'));
                $this->renderJSON($aResult);
                return;
            }
        }
        unset($packet['id']);
        $packet['updated_at'] = get_current_date();
        $result = $this->packet_model->updateRegisterPacket($shop_packet_id, $packet);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('update_success'));
        } else {
            $aResult = array('code' => 0, 'text' => $this->lang->line('update_fail'));
        }
        $this->renderJSON($aResult);
    }

    /**
     *@For ADMIN
     *@func Admin grid of checking if any shop register for new packet
     */
    public function acceptRegister()
    {
        $packet_id = 0;
        $packet = $this->input->post('packet', true);
        $tPacket = $this->input->post('packet');

        if (isset($tPacket['id']) && trim($tPacket['id']) != '' && isset($packet['active_flg']) &&
            trim($packet['active_flg']) != '') {
            $packet_id = $this->encrypt->decode($tPacket['id']);
            if (($packet['active_flg'] != 1 && $packet['active_flg'] != 0) || $packet_id == false) {
                $aResult = array('code' => 0, 'title' => $this->lang->line('insert_fail'));
                $this->renderJSON($aResult);
                return;
            }
        }
        unset($packet['id']);
        $packet['updated_at'] = get_current_date();
        $result = $this->packet_model->updateRegisterPacket($packet_id, $packet);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('update_success'));
        } else {
            $aResult = array('code' => 0, 'text' => $this->lang->line('update_fail'));
        }
        $this->renderJSON($aResult);
    }
}
?>